import * as Cesium from "cesium";
import gsap from "gsap";
export default class SpriteLineMaterial {
  constructor(name) {
    this.name = name;
    this.definitionChanged = new Cesium.Event();
    // 往Cesium 材质缓存中添加自己的材质
    Cesium.Material._materialCache.addMaterial("SpriteLineMaterial", {
      fabric: {
        type: "SpriteLineMaterial",
        uniforms: {
          uTime: 0,
          uImage: "/texture/spriteline1.png",
        },
        source: `
          czm_material czm_getMaterial(czm_materialInput materialInput)
          {
            // 生成默认的基础材质
            czm_material material = czm_getDefaultMaterial(materialInput);
            // uv
            vec2 st = materialInput.st;
            // 根据uv采样
            // 水平动态变化按照st.s - utime变化,不想要反方向运动所以是-utime,数值可能不是0-1的范围,所以使用fract函数返回小数部分
            vec4 color = texture2D(uImage,vec2(fract(st.s - uTime),st.t));
            material.alpha = color.a;
            material.diffuse = color.rgb;
            return material;
          }
        `,
      },
    });
    this.params = {
      uTime: 0,
    };
    gsap.to(this.params, {
      uTime: 1,
      duration: 1,
      repeat: -1,
      ease: "linear",
    });
  }
  getType(time) {
    return "SpriteLineMaterial";
  }
  getValue(time, result) {
    // 用来设置和处理uniform变量
    result.uTime = this.params.uTime;
    return result;
  }
  equals(other) {
    // 实现判断两个材质是否相等
    return other instanceof SpriteLineMaterial && this.name == other.name;
  }
}
